package com.pucheng.bracelet.app.util;


import com.blankj.utilcode.util.LogUtils;
import com.clj.fastble.utils.BleLog;
import com.clj.fastble.utils.HexUtil;

public class WriteUtils {

    public static byte[] getWriteBytes(String str){
        LogUtils.e("原数据："+str);
        byte rawData[] = ByteUtils.hexStringToBytes(str);
//        byte num1[] = ByteUtils.hexStr2Bytes("FB010000000000000000000000000000000000FA");
        byte lastData = rawData[0];
        for (int i = 1; i < rawData.length - 1; i++) {
            lastData = (byte) (lastData ^ rawData[i]);
        }
        rawData[rawData.length -1] = lastData;
        LogUtils.e("发送数据："+HexUtil.encodeHexStr(rawData));
        return rawData;
    }

//    public static byte[] getWriteCmdBytes(String str){
//        byte rawData[] = HexUtil.hexStringToBytes(str);
////        byte num1[] = ByteUtils.hexStr2Bytes("FB010000000000000000000000000000000000FA");
//        byte lastData = rawData[0];
//        for (int i = 1; i < rawData.length - 1; i++) {
//            lastData = (byte) (lastData ^ rawData[i]);
//        }
//        rawData[rawData.length -1] = lastData;
//        BleLog.e("发送数据："+HexUtil.encodeHexStr(rawData));
//        return rawData;
//    }

    /**
     * 获取更新资源的指令
     * @param resPackageInfo 资源包前12位字节
     * @return 指令数组
     */
    public static byte[] getUpDataCmdByte(byte resPackageInfo[]){
        byte rawData[] = new byte[20];
        rawData[0] = (byte) 0xFB;
        rawData[1] = (byte) 0xFC;
        for (int i = 0; i < resPackageInfo.length; i++) {
            rawData[i+2] = resPackageInfo[i];
        }
        byte lastData = rawData[0];
        for (int i = 1; i < rawData.length - 1; i++) {
            lastData = (byte) (lastData ^ rawData[i]);
        }
        rawData[rawData.length -1] = lastData;
        BleLog.e("发送数据："+HexUtil.encodeHexStr(rawData));
        return rawData;
    }
    public static byte[] getUpDataByte(byte resPackageInfo[],int mtuSize,int index){
        if (mtuSize>resPackageInfo.length){
            mtuSize = resPackageInfo.length;
        }
        byte rawData[] = new byte[mtuSize];
        rawData[0] = (byte)0xDA;
        byte rawData2[] = ByteUtils.subBytes(resPackageInfo,index*(mtuSize-1),mtuSize-1);
        for (int i = 0; i < rawData2.length; i++) {
            rawData[i+1] = rawData2[i];
        }
        LogUtils.e("资源更新数据包\n长度："+rawData.length+"\n数据："+HexUtil.encodeHexStr(rawData));
        return rawData;
    }

}
